package com.jbp.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.PrizePoolAmount;
import com.jbp.model.PrizePoolEntity;
import com.jbp.model.RankProportion;
import com.jbp.model.TopRooms;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 百万奖池金额更新，7天一周期处理，定时任务
 */
@Component
@Configuration
public class PrizePoolAmountJob {

    @Autowired
    private PrizePoolAmountService prizePoolAmountService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private LotteryRecordService lotteryRecordService;

    @Autowired
    private RankProportionService rankProportionService;

    @Autowired
    private TopRoomsService topRoomsService;


    //fixedRate = 7 * 24 * 60 * 60 * 1000  24 * 60 * 60 * 1000
//    @Scheduled(fixedDelay = 2 * 60 * 60 * 1000)
    @Scheduled(cron = "0 2 0 * * *")//  在00:02分
//    @Scheduled(cron = "0 49 11 * * *")//
    @Transactional
    public void checkPrizePoolAmount(){
        PrizePoolAmount prizePoolAmount = prizePoolAmountService.getOne(new QueryWrapper<PrizePoolAmount>().eq("flag", "1"));
        if (prizePoolAmount != null){
            Date createTime = prizePoolAmount.getCreateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(createTime);

            LocalDate pastDate = LocalDate.of(Integer.parseInt(time.split("-")[0]), Integer.parseInt(time.split("-")[1]), Integer.parseInt(time.split("-")[2]));
            LocalDate currentDate = LocalDate.now();
            Period period = Period.between(pastDate, currentDate);
            if (period.getYears() == 0 && period.getMonths() == 0 && period.getDays() == 7){//相隔7天
                //保存房间 排行榜数据，用作后期奖励
                String base_amount = propertyService.getV("base_amount");//百万奖池基础心钻
                Map<String, Object> params = new HashMap<>();
                SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String times = sdf.format(createTime);
                params.put("createTime", times);
                List<PrizePoolEntity> prizePoolEntityList = lotteryRecordService.prizePoolRanks(params);
                List<RankProportion> rankProportionList = rankProportionService.list();
                Map<Integer, Integer> mapProportion = new HashMap<>();
                if (rankProportionList != null && rankProportionList.size() > 0) {
                    for (RankProportion rankProportion : rankProportionList) {
                        mapProportion.put(rankProportion.getRanks(), rankProportion.getProportion());
                    }
                }
                if (prizePoolEntityList != null && prizePoolEntityList.size() > 0) {
                    List<TopRooms> topRoomsList = topRoomsService.list(new QueryWrapper<TopRooms>().eq("flag", "1"));
                    if (topRoomsList != null && topRoomsList.size() > 0) {
                        for (TopRooms topRooms : topRoomsList) {
                            topRooms.setFlag("0");
                            topRoomsService.updateById(topRooms);
                        }
                    }
                    BigDecimal totalAmount = new BigDecimal(base_amount).add(prizePoolAmount.getAmount());
                    for(int i = 1; i < prizePoolEntityList.size()+1; i++) {
                        PrizePoolEntity prizePoolEntity = prizePoolEntityList.get(i-1);
                        TopRooms topRooms = new TopRooms();
                        topRooms.setFlag("1");
                        topRooms.setBaseAmount(new BigDecimal(base_amount));
                        topRooms.setProportion(mapProportion.get(i));
                        topRooms.setContributeAmount(prizePoolAmount.getAmount());
                        topRooms.setAmount(totalAmount.multiply(new BigDecimal(topRooms.getProportion())).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        topRooms.setRoomId(prizePoolEntity.getId());
                        topRooms.setRoomName(prizePoolEntity.getRoomName());
                        topRooms.setImg(prizePoolEntity.getImg());
                        topRooms.setCreateTime(new Date());
                        topRooms.setPosition(i);
                        topRooms.setSchedule(time);
                        topRoomsService.save(topRooms);
                    }
                }

                //修改奖池贡献金额
                prizePoolAmount.setFlag("0");
                prizePoolAmount.setEndTime(new Date());
                prizePoolAmountService.updateById(prizePoolAmount);

                //新增奖池贡献金额
                prizePoolAmount = new PrizePoolAmount();
                prizePoolAmount.setFlag("1");
                prizePoolAmount.setAmount(new BigDecimal(0));
                prizePoolAmount.setCreateTime(new Date());
                prizePoolAmountService.save(prizePoolAmount);
            }
        }
        /*if (prizePoolAmount == null) {
            prizePoolAmount = new PrizePoolAmount();
            prizePoolAmount.setFlag("1");
            prizePoolAmount.setAmount(new BigDecimal(0));
            prizePoolAmount.setCreateTime(new Date());
            prizePoolAmountService.save(prizePoolAmount);
        }*/
    }

}
